﻿using GCC.Container.Abstracts;

using Proj4Net;

namespace GCC.APIContainer
{
    /// <summary>
    /// 容器上下文
    /// </summary>
    public partial class GCCContext : AbsGCCContext
    {

        /// <summary>
        /// 转换坐标系
        /// </summary>
        /// <param name="from">从哪个 比如  EPSG:3857</param>
        /// <param name="to">目标转换为 比如 EPSG:4326</param>
        /// <param name="sourceX">原始坐标X</param>
        /// <param name="sourceY">原始坐标Y</param>
        /// <param name="targetX">转换后X</param>
        /// <param name="targetY">转换后Y</param>
        public override void TransformCoordinate(string from, string to, double sourceX, double sourceY, out double targetX, out double targetY)
        {
            var ctFactory = new CoordinateTransformFactory();
            var csFactory = new CoordinateReferenceSystemFactory();
            var crs1 = csFactory.CreateFromName(from);
            var crs2 = csFactory.CreateFromName(to);

            var p1 = new ProjCoordinate
            {
                X = sourceX,
                Y = sourceY
            };
            var p2 = new ProjCoordinate();
            ICoordinateTransform trans = ctFactory.CreateTransform(crs1, crs2);
            _ = trans.Transform(p1, p2);
            targetX = p2.X;
            targetY = p2.Y;
        }

        /// <summary>
        /// 通过自定义算法来转坐标
        /// </summary>
        /// <param name="fromName">算法名字</param>
        /// <param name="fromContent">内容</param>
        /// <param name="toName">目标坐标系名字</param>
        /// <param name="toContext">目标坐标系算法</param>
        /// <param name="sourceX">原始坐标X</param>
        /// <param name="sourceY">原始坐标Y</param>
        /// <param name="targetX">转换后X</param>
        /// <param name="targetY">转换后Y</param>
        public override void TransformCoordinate(string fromName, string fromContent, string toName, string toContext, double sourceX, double sourceY, out double targetX, out double targetY)
        {
            var ctFactory = new CoordinateTransformFactory();
            var csFactory = new CoordinateReferenceSystemFactory();
            var crs1 = csFactory.CreateFromParameters(fromName, fromContent);
            var crs2 = csFactory.CreateFromParameters(toName, toContext);

            var p1 = new ProjCoordinate
            {
                X = sourceX,
                Y = sourceY
            };
            var p2 = new ProjCoordinate();
            ICoordinateTransform trans = ctFactory.CreateTransform(crs1, crs2);
            _ = trans.Transform(p1, p2);
            targetX = p2.X;
            targetY = p2.Y;
        }

    }
}
